<template>
  <view class="container">
    <!-- tab栏 -->
    <u-tabs
      :list="tabs"
      :is-scroll="false"
      :current="curTab"
      active-color="#FA2209"
      :duration="0.2"
      @change="tabChange"
    />

    <!-- 优惠券列表 -->
    <view class="coupon-list">
      <view
        class="coupon-item"
        v-for="item in coupon.data"
        :key="item.coupon_id"
      >
        <view class="item-wrapper" style="background-color: red">
          <view class="coupon-type" v-if="item.coupon_type === 10">满减券</view>
          <view class="coupon-types" v-if="item.coupon_type === 20"
            >折扣券</view
          >
          <view class="tip dis-flex flex-dir-column flex-x-center">
            <view v-if="item.reduce_price > 0">
              <text class="f-30">￥</text>
              <text class="money">{{ item.reduce_price }}</text>
            </view>
            <text class="money" v-if="item.discount > 0"
              >{{ item.discount }}折</text
            >
            <text class="pay-line">满{{ item.min_price }}元可用</text>
          </view>
          <view class="split-line"></view>
          <view class="content dis-flex flex-dir-column flex-x-between">
            <view class="title">{{ item.name }}</view>
            <view class="bottom dis-flex flex-y-center">
              <view class="time flex-box">
                <block v-if="item.expire_type === 10">
                  有效期 {{ item.expire_day }} 天</block
                >
                <block v-else
                  >{{ item.start_time }} ~ {{ item.end_time }}</block
                >
              </view>
              <view class="receive state">
                <text>{{ item.state.text }}</text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { list } from '@/api/myCoupon.js'
const color = ['red', 'blue', 'violet', 'yellow']
const tabs = [
  {
    name: `未使用`,
    value: 'isUnused'
  },
  {
    name: `已使用`,
    value: 'isUse'
  },
  {
    name: `已过期`,
    value: 'isExpire'
  }
]

export default {
  data () {
    return {
      // 颜色组
      color,
      // 标签栏数据
      tabs,
      curTab: 0,
      coupon: []
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.getMyCoupon()
  },
  methods: {
    // 获取我的优惠券
    async getMyCoupon () {
      const res = await list({ page: 1, dataType: 'isUnused' })
      console.log(res.data.list.data)
      this.coupon = res.data.list
    },
    // tab栏切换
    async tabChange (e) {
      this.curTab = e
      // 刷新数据
      // 获取当前点击的状态
      const dataType = this.tabs[this.curTab].value
      // 发送请求刷新
      const res = await list({ page: 1, dataType })
      this.coupon = res.data.list
    }
  }
}
</script>

<style lang="scss" scoped>
.coupon-list {
  padding: 20rpx;
}

.coupon-item {
  position: relative;
  overflow: hidden;
  margin-bottom: 22rpx;
}

.item-wrapper {
  width: 100%;
  display: flex;
  background: #fff;
  border-radius: 8rpx;
  color: #fff;
  height: 180rpx;

  .coupon-type {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 10;
    width: 128rpx;
    padding: 6rpx 0;
    background: #a771ff;
    font-size: 20rpx;
    text-align: center;
    color: #ffffff;
    transform: rotate(45deg);
    transform-origin: 64rpx 64rpx;
  }
  .coupon-types {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 10;
    width: 128rpx;
    padding: 6rpx 0;
    background: #55b5ff;
    font-size: 20rpx;
    text-align: center;
    color: #ffffff;
    transform: rotate(45deg);
    transform-origin: 64rpx 64rpx;
  }
  &.color-blue {
    background: linear-gradient(-125deg, #57bdbf, #2f9de2);
  }

  &.color-red {
    background: linear-gradient(-128deg, #ff6d6d, #ff3636);
  }

  &.color-violet {
    background: linear-gradient(-113deg, #ef86ff, #b66ff5);

    .coupon-type {
      background: #55b5ff;
    }
  }

  &.color-yellow {
    background: linear-gradient(-141deg, #f7d059, #fdb054);
  }

  &.color-gray {
    background: linear-gradient(-113deg, #bdbdbd, #a2a1a2);

    .coupon-type {
      background: #9e9e9e;
    }
  }

  .content {
    flex: 1;
    padding: 30rpx 20rpx;
    border-radius: 16rpx 0 0 16rpx;

    .title {
      font-size: 32rpx;
    }

    .bottom {
      .time {
        font-size: 24rpx;
      }

      .receive {
        height: 46rpx;
        width: 122rpx;
        line-height: 46rpx;
        text-align: center;
        border: 1rpx solid #fff;
        border-radius: 30rpx;
        color: #fff;
        font-size: 24rpx;

        &.state {
          border: none;
        }
      }
    }
  }

  .tip {
    position: relative;
    flex: 0 0 32%;
    text-align: center;
    border-radius: 0 16rpx 16rpx 0;

    .money {
      font-weight: bold;
      font-size: 52rpx;
    }

    .pay-line {
      font-size: 22rpx;
    }
  }

  .split-line {
    position: relative;
    flex: 0 0 0;
    border-left: 4rpx solid #fff;
    margin: 0 10rpx 0 6rpx;
    background: #fff;

    &:before {
      border-radius: 0 0 16rpx 16rpx;
      top: 0;
    }

    &:after {
      border-radius: 16rpx 16rpx 0 0;
      bottom: 0;
    }

    &:before,
    &:after {
      content: '';
      position: absolute;
      width: 24rpx;
      height: 12rpx;
      background: #f7f7f7;
      left: -14rpx;
      z-index: 1;
    }
  }
}
</style>
